package com.gitee.ricesugar.controller;

import com.gitee.ricesugar.common.Result;
import com.gitee.ricesugar.controller.request.UserPageRequest;
import com.gitee.ricesugar.entity.User;
import com.gitee.ricesugar.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    @PostMapping("/add")
    public Result add(@RequestBody User obj){
        userService.add(obj);
        return Result.success();
    }

    @PostMapping("/addlist")
    public Result addList(@RequestBody User[] obj){
        userService.addList(obj);
        return Result.success();
    }

    @GetMapping("/page")
    public Result page(UserPageRequest obj){
        return Result.success(userService.page(obj));
    }

    @GetMapping("/{uid}")
    public Result getByUid(@PathVariable String uid){
        User user = userService.getByUid(uid);
        return Result.success(user);
    }

    @GetMapping("/checkUsername/{username}")
    public Result checkUsername(@PathVariable String username){
        log.info(username);
        userService.checkUsernameExist(username);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody User obj){
        userService.update(obj);
        return Result.success();
    }

    @DeleteMapping("/delete/{uid}")
    public Result delete(@PathVariable String uid){
        userService.deleteByUid(uid);
        return Result.success();
    }
}
